<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Store_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();		
		$this->_table = $this->config->item('database_tables');
	}
	
	public function get_all_category()
	{
		$this->db->select('*');
		$this->db->from('category');
		$this->db->where('status', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_id_category_sitemap()
	{
		$this->db->select('id');
		$this->db->from('category');
		$this->db->where('status', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_product_of_category($alias, $limit, $start)
	{
		//echo $start; 
		//echo $categoryid; exit();
		$this->db->select('product.id, product.product_name, product.alias,  product.description, product.created_by, product.app_id, 
		product.category_id, product.hit, product.icon, product.featured, product.status, product.price, category.id as categoryid, category.category_name,category.alias as categoryalias');
		$this->db->join('category','category.id = product.category_id');
		$this->db->where('product.status', 1);
		$this->db->where('category.alias', $alias);
		$this->db->limit($limit, $start);
		$this->db->from('product','category');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_iphoneproduct_of_category($categoryid, $limit, $start)
	{
		$this->db->select('id, product_name, description, created_by, app_id, category_id, hit, icon, featured, status, price');
		$this->db->where('status', 1);
		$this->db->limit($limit, $start);
		$this->db->where('category_id', $categoryid);
		$this->db->where('app_id', 2);
		$query = $this->db->get('product');
		return $query->result_array();
	}
	
	public function count_all_product($alias)
	{
		$this->db->select('product.id, product.category_id, category.id as categoryid, category.alias as categoryalias');
		$this->db->join('category','category.id = product.category_id');
		$this->db->from('product','category');
		$this->db->where('category.alias', $alias);
		return $this->db->count_all_results();
	}
	
	public function get_product($productid)
	{
		$this->db->select('product.id, product.product_name, product.description, product.created_by, product.category_id, product.app_id, product.file,
		product.hit, product.icon, product.imgshow,product.featured, product.date_created, product.status, product.price, category.id, category.alias as categoryalias, category.category_name');
		$this->db->from('product','category');
		$this->db->join('category','category.id = product.category_id');
		$this->db->where('product.status', 1);
		$this->db->where('product.id', $productid);
		$this->db->limit(1);
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result[0];
		}
		return null;
	}
	
	public function get_related_items($id)
	{
		$obj = $this->get_product($id);
		$catid = $obj['category_id'];
		//echo $catid; exit();
		return $this->get_related_in_category($catid, 5);
		
	}
	
	public function get_related_in_category($id, $limit=0)
	{
		$this->db->select('id, product_name, description, alias, created_by, app_id, category_id, hit, icon, featured, status, price');
		$this->db->where('status', 1);
		$this->db->where('category_id', $id);
		if($limit!=0)
		{
			$this->db->limit($limit);
		}
		$this->db->order_by('id', 'DESC');
		$query = $this->db->get('product');
		return $query->result_array();
	}
	
	function update_hit($productid)
	{
		$this->db->where('id', $productid);
    	$this->db->set('hit', 'hit+1', FALSE);
    	$this->db->update('product');
	}
	
	public function get_featured_product()
	{
		$this->db->select('id, product_name, description, created_by, category_id, hit, icon, featured, status, price');
		$this->db->where('featured', 1);
		$this->db->where('status', 1);
		$query = $this->db->get('product');
		return $query->result_array();
	}
	
	public function get_game_android_product()
	{
		$this->db->select('*');
		$this->db->where('app_id', 1);
		$query = $this->db->get('product');
		return $query->result_array();
	}
	// =============================
	// = ========== GET ========== =
	// =============================
	
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
}
// END homepage_model.php
/* Location: ./application/modules/home/models/homepage_model.php */